/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.gar.bc;

import edu.upn.sigemfi.gar.be.Aval;
import edu.upn.sigemfi.gar.be.Garantia;
import edu.upn.sigemfi.gar.be.GarantiaHipotecaria;
import edu.upn.sigemfi.gar.be.GarantiaLiquida;
import edu.upn.sigemfi.gar.be.GarantiaPignoraticia;
import edu.upn.sigemfi.gar.be.GarantiaPrendaria;
import edu.upn.sigemfi.gar.be.GarantiaWarrant;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Programador
 */
public class GarantiaDelegate {

    private GarantiaFacadeLocal local;

    public GarantiaDelegate(){
        local = lookupGarantiaFacade();
    }

    //el lookup es el Garantia
    private GarantiaFacadeLocal lookupGarantiaFacade() {
        try {
            Context c = new InitialContext();
            return (GarantiaFacadeLocal) c.lookup("java:comp/env/GarantiaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    //Garantia Prendaria
    public void crearGarantiaPrendaria(GarantiaPrendaria garantiaPrendaria) throws Exception
    {
        try{
            local.create(garantiaPrendaria);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Crear Garantia Prendaria"+ex.getMessage());
        }
    }

    public void editarGarantiaPrendaria(GarantiaPrendaria garantiaPrendaria) throws Exception
    {
        try{
            local.edit(garantiaPrendaria);
        }catch (Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Editar GarantiaPrendaria"+ex.getMessage());
        }
    }

    public void eliminarGarantiaPrendaria(GarantiaPrendaria garantiaPrendaria) throws Exception
    {
        try{
            local.remove(garantiaPrendaria);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Crear GarantiaPrendaria"+ex.getMessage());
        }
    }

    public GarantiaPrendaria buscarPrendaria(Long codigo) throws Exception {
        try {
            return local.findPrendaria(codigo);
        } catch (Exception e) {
            local = lookupGarantiaFacade();
            throw new Exception("Buscar GarantiaPrendaria. " + e.getMessage(), e);
        }
    }

    public List<GarantiaPrendaria> listarPrendarias() throws Exception {
        try {
            return local.findAllPrendarias();
        } catch (Exception e) {
            local = lookupGarantiaFacade();
            throw new Exception("Listar GarantiasPrendarias. " + e.getMessage(), e);
        }
    }

    //Aval
    public void crearAval(Aval aval) throws Exception
    {
        try{
            local.create(aval);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Crear Aval"+ex.getMessage());
        }
    }

    public void editarAval(Aval aval) throws Exception
    {
        try{
            local.edit(aval);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Editar Aval"+ex.getMessage());
        }
    }

    public void eliminarAval(Aval aval) throws Exception
    {
        try{
            local.remove(aval);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Eliminar Aval"+ex.getMessage());
        }
    }

     public Aval buscarAval(Long codigo) throws Exception {
        try {
            return local.findAval(codigo);
        } catch (Exception e) {
            local = lookupGarantiaFacade();
            throw new Exception("Buscar Aval. " + e.getMessage(), e);
        }
    }

    public List<Aval> listarAvales() throws Exception {
        try {
            return local.findAllAvales();
        } catch (Exception e) {
            local = lookupGarantiaFacade();
            throw new Exception("Listar GarantiasPrendarias. " + e.getMessage(), e);
        }
    }

    //Garantia Hipotecaria
    public void crearGarantiaHipotecaria(GarantiaHipotecaria garantiaHipotecaria) throws Exception
    {
        try{
            local.create(garantiaHipotecaria);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Crear Hipotecaria"+ex.getMessage());
        }
    }

    public void editarGarantiaHipotecaria(GarantiaHipotecaria garantiaHipotecaria) throws Exception
    {
        try{
            local.edit(garantiaHipotecaria);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Editar Hipotecaria"+ex.getMessage());
        }
    }

    public void eliminarGarantiaHipotecaria(GarantiaHipotecaria garantiaHipotecaria) throws Exception
    {
        try{
            local.remove(garantiaHipotecaria);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Eliminar Hipotecaria"+ex.getMessage());
        }
    }

    public List<GarantiaHipotecaria> listarHipotecarias() throws Exception {
        try {
            return local.findAllHipotecarias();
        } catch (Exception e) {
            local = lookupGarantiaFacade();
            throw new Exception("Listar GarantiasHipotecarias. " + e.getMessage(), e);
        }
    }

    //Garantia Liquida
    public void crearGarantiaLiquida(GarantiaLiquida garantiaLiquida) throws Exception
    {
        try{
            local.create(garantiaLiquida);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Crear Liquida"+ex.getMessage());
        }
    }

    public void editarGarantiaLiquida(GarantiaLiquida garantiaLiquida) throws Exception
    {
        try{
            local.edit(garantiaLiquida);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Editar Liquida"+ex.getMessage());
        }
    }

    public void eliminarGarantiaLiquida(GarantiaLiquida garantiaLiquida) throws Exception
    {
        try{
            local.remove(garantiaLiquida);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Eliminar Liquida"+ex.getMessage());
        }
    }

    //Garantia Pignoraticia
    public void crearGarantiaPignoraticia(GarantiaPignoraticia garantiaPignoraticia) throws Exception
    {
        try{
            local.create(garantiaPignoraticia);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Crear Pignoraticia"+ex.getMessage());
        }
    }

    public void editarGarantiaPignoraticia(GarantiaPignoraticia garantiaPignoraticia) throws Exception
    {
        try{
            local.edit(garantiaPignoraticia);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Crear Pignoraticia"+ex.getMessage());
        }
    }

    public void eliminarGarantiaPignoraticia(GarantiaPignoraticia garantiaPignoraticia) throws Exception
    {
        try{
            local.remove(garantiaPignoraticia);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Crear Pignoraticia"+ex.getMessage());
        }
    }

    //Garantia Warrant
    public void crearGarantiaWarrant(GarantiaWarrant garantiaWarrant) throws Exception
    {
        try{
            local.create(garantiaWarrant);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Crear Warrant"+ex.getMessage());
        }
    }

    public void editarGarantiaWarrant(GarantiaWarrant garantiaWarrant) throws Exception
    {
        try{
            local.edit(garantiaWarrant);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Editar Warrant"+ex.getMessage());
        }
    }

    public void eliminarGarantiaWarrant(GarantiaWarrant garantiaWarrant) throws Exception
    {
        try{
            local.remove(garantiaWarrant);
        }
        catch(Exception ex){
            local=lookupGarantiaFacade();
            throw new Exception("Eliminar Warrant"+ex.getMessage());
        }
    }
}
